package com.fable.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyResourceConfigurer;

import java.util.Enumeration;
import java.util.Properties;

/**
 * 类说明(异常码转换工具)
 */
public class ConvertErrorCodeUtil extends PropertyResourceConfigurer {
    /**
     * 返回至页面的提示信息主键
     */
    public static final String ERROR_MESSAGE = "error_msg";
    /**
     * 返回至页面的操作是否成功的标志位主键
     */
    public static final String SUCCESS = "success";

    /**
     * logback需要使用slf4j
     */
    private static final Logger LOG = LoggerFactory.getLogger(ConvertErrorCodeUtil.class);
    private static Properties properties;

    public static String getErrorMessage(String code) {
        return (String) properties.get(code);
    }

    public static String getErrorMessage(String code, String defaultValue) {
        return properties.getProperty(code, defaultValue);
    }

    public static void appendProperties(Properties paramProperties) {
        if (properties == null) {
            properties = paramProperties;
        } else {
            Enumeration enums = paramProperties.propertyNames();
            while (enums.hasMoreElements()) {
                String code = (String) enums.nextElement();
                properties.setProperty(code, paramProperties.getProperty(code));
                LOG.info("add ConvertErrorCodeUtil:  " + code + " = '" + paramProperties.getProperty(code));
            }
        }
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory configurablelistablebeanfactory, Properties properties) throws BeansException {
        appendProperties(properties);
    }
}
